package business.knBase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import business.database.Database;

/**
 * Esta clase almacena en memoria toda la informacion de KnBase
 * @author Administrador
 *
 */
public class KnBase {
	
	private static KnBase _instance;
	
	private List<String> excludes;
	private List<String> countryWord;
	private List<String> foreignWithSpain;
	private HashMap<String,String> foreignWords;
	private HashMap<String,String> provWords;
	private HashMap<String,String> regions;
	private HashMap<String,String> sectors;
	private HashMap<String,String> spanishForeigns;
	
	public static KnBase getInstance() {
		if (_instance == null) _instance = new KnBase();
		
		return _instance;
	}
	private KnBase() {
		excludes = new ArrayList<String>();
		countryWord = new ArrayList<String>();
		foreignWithSpain = new ArrayList<String>();
		foreignWords = new HashMap<String,String>();
		provWords = new HashMap<String,String>();
		regions = new HashMap<String,String>();
		sectors = new HashMap<String,String>();
		spanishForeigns = new HashMap<String,String>();
		
		fillStructures();
	}
	
	private void fillStructures() {
		fillExcludes();
		fillCountryWord();
		fillForeignWithSpain();
		fillForeignWords();
		fillProvWords();
		fillRegions();
		fillSectors();
		fillSpanishForeigns();
	}
	
	private void fillExcludes() {
		Database.getInstance().getSqlKnBase().eachRow("select * from excludes", {
			excludes.add(it[0]);
			});
	}
	
	private void fillCountryWord() {
		Database.getInstance().getSqlKnBase().eachRow("select * from countryWord", {
			countryWord.add(it[0]);
			});
	}
	private void fillForeignWithSpain() {
		Database.getInstance().getSqlKnBase().eachRow("select * from foreignWithSpain", {
			foreignWithSpain.add(it[0]);
			});
	}
	private void fillForeignWords() {
		Database.getInstance().getSqlKnBase().eachRow("select word, code from foreignWords", {
			String word = it[0]
			String code = it[1]
			if (!foreignWords.containsKey(word))
				foreignWords.put(word,code);
			});
	}
	private void fillProvWords() {
		Database.getInstance().getSqlKnBase().eachRow("select word, code from foreignWords", {
			String word = it[0]
			String code = it[1]
			if (!foreignWords.containsKey(word))
				foreignWords.put(word,code);
			});
	}
	private void fillRegions() {
		Database.getInstance().getSqlKnBase().eachRow("select region, prov from regions", {
			String region = it[0]
			String prov = it[1]
			if (!regions.containsKey(region))
				regions.put(region, prov);
			});
	}
	private void fillSectors() {
		Database.getInstance().getSqlKnBase().eachRow("select instit, sector from sector", {
			String instit = it[0]
			String sector = it[1]
			if (!sectors.containsKey(instit))
				sectors.put(instit, sector);
			});
	}
	private void fillSpanishForeigns() {
		Database.getInstance().getSqlKnBase().eachRow("select word, code from foreignWords", {
			String word = it[0]
			String code = it[1]
			if (!foreignWords.containsKey(word))
				foreignWords.put(word,code);
			});
	}
	
	

}
